package com.bjpowernode.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.bjpowernode.constant.BannerConstant;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bjpowernode.domain.Banner;
import com.bjpowernode.mapper.BannerMapper;
import com.bjpowernode.service.BannerService;
@Service
@CacheConfig(cacheNames = "com.bjpowernode.service.impl.BannerServiceImpl") // 开启Springboot缓存
public class BannerServiceImpl extends ServiceImpl<BannerMapper, Banner> implements BannerService{
    @Resource
    private BannerMapper bannerMapper;
    /**
     * 查询轮播图接口
     *
     * @return
     */
    @Override
    @Cacheable(key = BannerConstant.BANNER_CACHE_PREFIX) // 缓存时间
    public List<String> selectBanner() {
        // List<String> paths = bannerMapper.selectBanner(); 第一种写法

        // 第二种写法查询全部数据
        /*List<Banner> banners = bannerMapper.selectList(new LambdaQueryWrapper<Banner>()
                .orderByDesc(Banner::getBeginTime)
                .last(" limit 5 ") //  在last里面可以随便写sql
        );
        List<String> paths = banners.stream().map(Banner::getPath).collect(Collectors.toList());*/

        // 第三种写法查指定数据
        List<Object> objects = bannerMapper.selectObjs(new LambdaQueryWrapper<Banner>()
                .select(Banner::getPath) // 使用select表示只查这个字段
                .orderByDesc(Banner::getBeginTime)
                .last(" limit 5 ")
        );
        List<String> paths = objects.stream().map(Object::toString).collect(Collectors.toList());
        return paths;
    }
}
